package com.javabasic.algorithm.baidu_interview;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/2/24 6:36 下午
 */
public class ListTest {
    public static void main(String[] args) {
        CustomLinkedList customLinkedList = new CustomLinkedList();
        CustomNode customNode = new CustomNode(1, null);
        new CustomNode(2,null);
    }
}

class CustomLinkedList {
    private CustomNode head;
    private CustomNode tail;
    int size;

    public CustomLinkedList() {
        head = tail= null;
        size = -1;
    }

    public void add(CustomNode node) {
        if (null == node) {
            return;
        }
        if (size == -1) {
            head = tail = node;
        } else {
            node.setNext(null);
            tail.setNext(node);
        }
        size++;
    }

    public boolean  deleteOfIndex(int index) {
        if (index > size || size < 0) {
            return false;
        }
        CustomNode tempPre = head, currentNode = head;
        while (index >= 0 && currentNode.getNext() != null) {
            tempPre = currentNode;
            currentNode = currentNode.getNext();
            index--;
        }
        tempPre.setNext(currentNode.getNext());

        return true;
    }
}

class CustomNode {
    private int value;
    private CustomNode next;

    public CustomNode() {
    }

    public CustomNode(int value, CustomNode next) {
        this.value = value;
        this.next = next;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public CustomNode getNext() {
        return next;
    }

    public void setNext(CustomNode next) {
        this.next = next;
    }
}
